#include <dbg.h>
#include <gtest/gtest.h>
using namespace std;

class Solution {
public:
    int maximumSum(vector<int>& nums) {
        int res = -1;

        vector<int> record(82, 0);  // 1 <= nums[i] <= 10^9
        for (int num : nums) {
            int sum = bitSum(num);
            if (record[sum]) {
                res = max(res, record[sum] + num);
            }
            record[sum] = max(record[sum], num);
        }
        return res;
    }

private:
    int bitSum(int x) {
        int sum = 0;
        while (x > 0) {
            sum += x % 10;
            x /= 10;
        }
        dbg(sum);
        return sum;
    }
};

TEST(MAXIMUM_SUM_Test1, maximumSum) {
    Solution s;

    vector<int> nums{18, 43, 36, 13, 7};
    EXPECT_EQ(s.maximumSum(nums), 54);
}

TEST(MAXIMUM_SUM_Test2, maximumSum) {
    Solution s;

    vector<int> nums{10, 12, 19, 14};
    EXPECT_EQ(s.maximumSum(nums), -1);
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}